#! /bin/sh

if [ -z "$1" -o -n "$2" ]; then
   echo "Usage: $0 new_tag" >&2
   exit 1
else
   tag="$1"
fi

. `dirname $0`/split_tag.sh &&
split_tag $tag

if [ $micro -eq 0 -o \( "$state" != final -a "$state" != post \) ]; then
   git checkout master
else
   git checkout maint
fi &&

chth_tag="Cheetah Template $tag" &&
chth_tag_len=${#chth_tag} &&
sed -Ei "1s/^Cheetah Template [1-9].+\$/$chth_tag/" README.rst &&
sed -Ei "2s/^==========+\$/`python -c \"print('='*$chth_tag_len)\"`/" README.rst &&

if [ "$state" = alpha ]; then
    trove_cls='3 - Alpha'
elif [ "$state" = beta -o "$state" = 'release candidate' ]; then
    trove_cls='4 - Beta'
elif [ "$state" = final -o "$state" = post ]; then
    trove_cls='5 - Production\/Stable'
else
    echo "Error: unknown state $state" >&2
    exit 1
fi &&
sed -Ei "s/Development Status :: .+\$/Development Status :: $trove_cls/" SetupConfig.py &&

if [ "$state" = final -o "$state" = post ]; then
    ghr_prerelease=false
else
    ghr_prerelease=true
fi &&
sed -Ei "s/(^ +prerelease): .*\$/\1: $ghr_prerelease/" .github/workflows/test-publish.yaml &&

`git var GIT_EDITOR` docs/news.rst ANNOUNCE.rst LATEST-CHANGES.rst Cheetah/Version.py README.rst setup.cfg SetupConfig.py &&
exec git commit --message="Release $tag" docs/news.rst ANNOUNCE.rst LATEST-CHANGES.rst Cheetah/Version.py README.rst setup.cfg SetupConfig.py .github/workflows/test-publish.yaml
